package com.study.thread;

/**
 * @Description: 买票
 * @Param:
 * @return:
 * @Author: 朝花不迟暮
 * @Date: 2020/6/2
 */
public class SaleMain
{
    public static void main(String[] args)
    {
        Station station1 = new Station("窗口1");
        Station station2 = new Station("窗口2");
        Station station3 = new Station("窗口3");
        // 让每一个站台对象各自开始工作
        station1.start();
        station2.start();
        station3.start();
    }
}

class Station extends Thread
{
    public Station(String name)
    {
        super(name);
    }

    // 为了保持票数的一致，票数要静态
    static int tick = 20;

    // 创建一个静态钥匙
    static final Object ob = "aa";//值是任意的

    @Override
    public void run()
    {
        while (tick > 0)
        {
            synchronized (ob)
            {
                if (tick > 0)
                {
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + tick + "张票");
                    tick--;
                }
               if(tick==0){
                   System.out.println("卖完了！");
               }
            }
            try {
                sleep(1000); //休息一秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}